package com.wutianyi.module.dao.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wutianyi.module.commons.config.ConfigDO;
import com.wutianyi.module.commons.config.ConfigTypeEnum;

/**
 * @author hanjiewu
 * 
 */
public class ConfigDaoMysqlImpl implements ConfigDao {

	private ConfigMapper configMapper;

	@Override
	public List<ConfigDO> listConfigsByType(ConfigTypeEnum type) {

		List<ConfigDO> configs = configMapper.listConfigsByType(type.getType());

		Map<Integer, ConfigDO> maps = new HashMap<Integer, ConfigDO>();
		List<ConfigDO> rconfigs = new ArrayList<ConfigDO>();
		for (ConfigDO config : configs) {
			if (config.getpId() == 0) {
				maps.put(config.getId(), config);
				rconfigs.add(config);
			}
		}

		for (ConfigDO config : configs) {
			if (config.getpId() != 0) {
				ConfigDO parent = maps.get(config.getpId());
				if (null != parent) {
					parent.addChild(config);
				}
			}
		}

		return rconfigs;
	}

	public ConfigMapper getConfigMapper() {
		return configMapper;
	}

	public void setConfigMapper(ConfigMapper configMapper) {
		this.configMapper = configMapper;
	}

}
